package com.showde.shoppingmall.utils;

import java.io.IOException;
import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import com.kim.lang.IOUtils;
import com.showde.shoppingmall.ShoppingMallApplication;

public class Utils {

	public static final String convertInputStream2String(
			InputStream inputStream, boolean gzip) throws IOException {
		byte[] bytes = null;

		if (gzip) {
			bytes = IOUtils.getGZipBytes(inputStream);
		} else {
			bytes = IOUtils.getByteByStream(inputStream);
		}

		if (bytes != null) {
			return new String(bytes, "UTF-8");
		}
		return null;
	}

	public static final JSONObject string2JSON(String jsonStr) {
		if (jsonStr == null || "".equals(jsonStr)) {
			LogRecorder.w(Utils.class.getName(), "empty jsonStr");
			return null;
		}
		try {
			return new JSONObject(jsonStr);
		} catch (JSONException e) {
			if (ShoppingMallApplication.DEVMode) {
				LogRecorder.w(Utils.class.getName(), "bad json structure");
				e.printStackTrace();
			}
		}
		return null;
	}
}
